package gov.nhso.prov.acc.utils;

import gov.nhso.prov.acc.core.ConfigBundle;

import com.isud.core.ISUD;

public class GenCode {
	private String schema = ConfigBundle.getValue("SCHEMA");
	private DigitUtil digitUtil = new DigitUtil(); 
	private ISUD dao = null;
	public GenCode(ISUD dao) {
		this.dao = dao;
	}
	
	public synchronized String getRoleId() {
		String result = "";
		try {
			result = this.dao.chkData(this.dao.findDataSql("SELECT MAX(ROLE_ID) + 1 AS ID FROM " + this.schema + ".ACC67_ROLE").get("ID"));
			if (result.equals("")) {
				result = "0";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public synchronized String getIncomeDocNoAcc7(String budgetYear, String provinceId,String type) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(DOC_NO) AS DOC_NO FROM " + this.schema + ".ACC7_INCOME WHERE BUDGET_YEAR = " + budgetYear + " AND PROVINCE_ID = '" + provinceId + "' AND DOC_NO LIKE 'ACC7"+budgetYear+type+provinceId+"%'").get("DOC_NO"));
			if (!docNo.equals("")) {
				String prefix = docNo.substring(0, docNo.length() - 11) + type;
				int running = Integer.parseInt(docNo.substring(docNo.length() - 6, docNo.length()));
				result = prefix + provinceId + this.digitUtil.numberFormat("000000", String.valueOf(running + 1));
			} else {
				result = "ACC7" + budgetYear + type + provinceId + "000001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public synchronized String getExpenseDocNoAcc7(String budgetYear, String provinceId,String type) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(DOC_NO) AS DOC_NO FROM " + this.schema + ".ACC7_EXPENSE WHERE BUDGET_YEAR = " + budgetYear + " AND PROVINCE_ID = '" + provinceId + "'").get("DOC_NO"));
			if (!docNo.equals("")) {
				String prefix = docNo.substring(0, docNo.length() - 11) + type;
				int running = Integer.parseInt(docNo.substring(docNo.length() - 6, docNo.length()));
				result = prefix + provinceId + this.digitUtil.numberFormat("000000", String.valueOf(running + 1));
			} else {
				result = "ACC7" + budgetYear + type + provinceId + "000001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public synchronized String getIncomeDocNo(String budgetYear, String provinceId,String type) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(DOC_NO) AS DOC_NO FROM " + this.schema + ".ACC6_INCOME WHERE BUDGET_YEAR = " + budgetYear + " AND PROVINCE_ID = '" + provinceId + "' AND INCOME_TYPE = '"+type+"' ").get("DOC_NO"));
			if (!docNo.equals("")) {
				String prefix = docNo.substring(0, docNo.length() - 11) + type;
				int running = Integer.parseInt(docNo.substring(docNo.length() - 6, docNo.length()));
				result = prefix + provinceId + this.digitUtil.numberFormat("000000", String.valueOf(running + 1));
			} else {
				result = "ACC6" + budgetYear + type + provinceId + "000001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public synchronized String getExpenseDocNo(String budgetYear, String provinceId,String type) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(DOC_NO) AS DOC_NO FROM " + this.schema + ".ACC6_EXPENSE WHERE BUDGET_YEAR = " + budgetYear + " AND PROVINCE_ID = '" + provinceId + "' AND EXPENSE_TYPE = '"+type+"'").get("DOC_NO"));
			if (!docNo.equals("")) {
				String prefix = docNo.substring(0, docNo.length() - 6);
				int running = Integer.parseInt(docNo.substring(docNo.length() - 6, docNo.length()));
				result = prefix + this.digitUtil.numberFormat("000000", String.valueOf(running + 1));
			} else {
				result = "ACC6" + budgetYear + type + provinceId + "000001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public synchronized String getExpenseOtherDocNo(String budgetYear, String provinceId,String type) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(DOC_NO) AS DOC_NO FROM " + this.schema + ".ACC6_EXPENSE_OTHER WHERE BUDGET_YEAR = " + budgetYear + " AND PROVINCE_ID = '" + provinceId + "' AND EXPENSE_TYPE = '"+type+"'").get("DOC_NO"));
			if (!docNo.equals("")) {
				String prefix = docNo.substring(0, docNo.length() - 6);
				int running = Integer.parseInt(docNo.substring(docNo.length() - 6, docNo.length()));
				result = prefix + this.digitUtil.numberFormat("000000", String.valueOf(running + 1));
			} else {
				result = "ACC6" + budgetYear + type + provinceId + "000001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public synchronized String getCuequeDocNo(String budgetYear, String provinceId,String type) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(DOC_NO) AS DOC_NO FROM " + this.schema + ".ACC6_CHEQUE WHERE PROVINCE_ID = '" + provinceId + "'").get("DOC_NO"));
			if (!docNo.equals("")) {
				String prefix = docNo.substring(0, docNo.length() - 5);
				int running = Integer.parseInt(docNo.substring(docNo.length() - 5, docNo.length()));
				result = prefix + this.digitUtil.numberFormat("00000", String.valueOf(running + 1));
			} else {
				result = "ACC6" + budgetYear + type + provinceId + "L00001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public synchronized String getVenderCode(String provinceId) {
		String result = "";
		try {
			String docNo = this.dao.chkData(this.dao.findDataSql("SELECT MAX(VENDER_CODE) AS VENDER_CODE FROM " + this.schema + ".ACC6_VENDER WHERE PROVINCE_ID = '" + provinceId + "' AND LENGTH(VENDER_CODE) = 10 ").get("VENDER_CODE"));
			if (!docNo.equals("")) {
				String prefix = provinceId;
				int running = Integer.parseInt(docNo.substring(docNo.length() - 6, docNo.length()));
				result = prefix + this.digitUtil.numberFormat("000000", String.valueOf(running + 1));
			} else {
				result = provinceId + "000001";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public synchronized String getExpenseCategoryId() {
		String result = "";
		try {
			result = this.dao.chkData(this.dao.findDataSql("SELECT MAX(CATEGORY_ID) + 1 AS ID FROM " + this.schema + ".ACC67_EXPENSE_CATEGORY").get("ID"));
			if (result.equals("")) {
				result = "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
